import Vue from 'vue'
import VueRouter from 'vue-router'

//懒加载
const Home =()=>import('@/views/home/Home');
const Cart =()=>import('@/views/cart/Cart');
const Category =()=>import('@/views/category/Category');
const Profile =()=>import('@/views/profile/Profile');

Vue.use(VueRouter)

  const routes = [
    {
      path:'',
      redirect:'/home'
    },
    {
      path: '/home',
      component:Home,
      meta:{
        title:'首页'
      }
    },
    {
      path: '/cart',
      component:Cart,
      meta:{
        title:'购物车'
      }
    },
    {
      path: '/category',
      component:Category,
      meta:{
        title:'分类'
      }
    },
    {
      path: '/profile',
      component:Profile,
      meta:{
        title:'我的'
      }
    }
]

const router = new VueRouter({
  routes,
  mode:'history'
})

//全局
//前置守卫(guard)
router.beforeEach((to,from,next)=>{
  //to 和 from都是一个完整的路由对象，通过获取路由对象里的title，达到更新页面标题的目的
  document.title=to.matched[0].meta.title;
  //next必须被执行一次
  next();
})

//全局
//后置钩子(hook)，无next
router.afterEach((to,from)=>{

})

export default router
